package com.wzx.core.ioc.proxy;

import net.sf.cglib.proxy.InvocationHandler;

import java.lang.reflect.Method;

public abstract class Invocation implements InvocationHandler {

    protected abstract void before(Object o, Method method, Object[] objects);
    protected abstract void after(Object o, Method method, Object[] objects);
    protected abstract void running(Object o, Method method, Object[] objects, Object retVal);
    protected abstract void throwing(Object o, Method method, Object[] objects, Throwable ex);
    protected abstract Object getOriginObject();


    @Override
    public Object invoke(Object o, Method method, Object[] objects) {
        this.before(this.getOriginObject(), method, objects);
        try {
            Object result = method.invoke(this.getOriginObject(), objects);
            this.running(this.getOriginObject(), method, objects, result);
            this.after(this.getOriginObject(), method, objects);
            return result;
        } catch (Throwable e) {
            this.throwing(this.getOriginObject(), method, objects, e);
            throw new RuntimeException(e);
        }
    }
}
